﻿Imports DTO
Imports DAL
Public Class ClienteBLL

#Region "Métodos"

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Sub Incluir(cliente As ClienteDTO)
        Dim dalCliente As New ClienteDAL
        Dim dtoCliente As New ClienteDTO
        Try
            If ValidaCPF(cliente.pCpf) Then
                dtoCliente = dalCliente.Consultar(cliente.pCpf)
                If IsNothing(dtoCliente) Then
                    dalCliente.Incluir(cliente)
                Else
                    Throw New Exception("Cliente já cadastrado em nossa base de dados")
                End If
            End If
        Catch ex As Exception
            Throw ex
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="cliente"></param>
    ''' <remarks></remarks>
    Sub Alterar(cliente As ClienteDTO)
        Dim dalCliente As New ClienteDAL

        Try
            dalCliente.Alterar(cliente)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="cpf"></param>
    ''' <remarks></remarks>
    Sub Excluir(cpf As String)
        Dim dalCliente As New ClienteDAL
        Try
            dalCliente.Excluir(cpf)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="cpf"></param>
    ''' <remarks></remarks>
    Sub Consultar(cpf As String)
        Dim dalCliente As New ClienteDAL
        Try
            dalCliente.Consultar(cpf)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function Listar() As List(Of ClienteDTO)
        Dim dtoCliente As List(Of ClienteDTO)
        Dim dalCliente As New ClienteDAL

        Try

            dtoCliente = dalCliente.Listar()
            Return dtoCliente
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Function ValidaCPF(ByVal CPF As String) As Boolean

        Dim strCPFOriginal As String = CPF.Replace(".", "").Replace("-", "")
        Dim strCPF As String = Mid(strCPFOriginal, 1, 9)
        Dim strCPFTemp As String
        Dim intSoma As Integer
        Dim intResto As Integer
        Dim strDigito As String
        Dim intMultiplicador As Integer = 10
        Const constIntMultiplicador As Integer = 11
        Dim i As Integer
        '--------------------------

        For i = 0 To strCPF.ToString.Length - 1
            intSoma += CInt(strCPF.ToString.Chars(i).ToString) * intMultiplicador
            intMultiplicador -= 1
        Next

        If (intSoma Mod constIntMultiplicador) < 2 Then
            intResto = 0
        Else
            intResto = constIntMultiplicador - (intSoma Mod constIntMultiplicador)
        End If

        strDigito = intResto
        intSoma = 0

        strCPFTemp = strCPF & strDigito
        intMultiplicador = 11

        For i = 0 To strCPFTemp.Length - 1
            intSoma += CInt(strCPFTemp.Chars(i).ToString) * intMultiplicador
            intMultiplicador -= 1
        Next

        If (intSoma Mod constIntMultiplicador) < 2 Then
            intResto = 0
        Else
            intResto = constIntMultiplicador - (intSoma Mod constIntMultiplicador)
        End If

        strDigito &= intResto

        If strDigito = Mid(strCPFOriginal, 10, strCPFOriginal.Length) Then
            Return True
        Else
            Return False
        End If
    End Function

#End Region

End Class

